﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockLib;

namespace StockUI
{
    public partial class ControlTrade : UserControl
    {
        private Stock _stock = null;

        private List<KData> _kData;
        public List<KData> KData
        {
            get
            {
                return this._kData;
            }
            set
            {
                this._kData = value;

                if (value != null)
                {
                    gridMinute.DataSource = value.OrderByDescending(d => d.Time).ToList();
                }
            }
        }

        public Stock stock
        {
            get { return this._stock; }
            set
            {
                this._stock = value;

                label1.ForeColor = StockProfile.Colors.GraphTextColor;
                label6.ForeColor = StockProfile.Colors.GraphTextColor;
                label7.ForeColor = StockProfile.Colors.GraphTextColor;
                label8.ForeColor = StockProfile.Colors.GraphTextColor;
                label9.ForeColor = StockProfile.Colors.GraphTextColor;
                label10.ForeColor = StockProfile.Colors.GraphTextColor;
                label12.ForeColor = StockProfile.Colors.GraphTextColor;
                label13.ForeColor = StockProfile.Colors.GraphTextColor;
                label14.ForeColor = StockProfile.Colors.GraphTextColor;
                label16.ForeColor = StockProfile.Colors.GraphTextColor;
                label18.ForeColor = StockProfile.Colors.GraphTextColor;
                label20.ForeColor = StockProfile.Colors.GraphTextColor;
                label22.ForeColor = StockProfile.Colors.GraphTextColor;
                label24.ForeColor = StockProfile.Colors.GraphTextColor;
                label25.ForeColor = StockProfile.Colors.GraphTextColor;
                label26.ForeColor = StockProfile.Colors.GraphTextColor;
                label27.ForeColor = StockProfile.Colors.GraphTextColor;
                label28.ForeColor = StockProfile.Colors.GraphTextColor;
                label29.ForeColor = StockProfile.Colors.GraphTextColor;
                label31.ForeColor = StockProfile.Colors.GraphTextColor;
                label33.ForeColor = StockProfile.Colors.GraphTextColor;
                label34.ForeColor = StockProfile.Colors.GraphTextColor;
                label35.ForeColor = StockProfile.Colors.GraphTextColor;

                lblSeperator1.BackColor = StockProfile.Colors.AxisLineColor;
                lblSeperator2.BackColor = StockProfile.Colors.AxisLineColor;
                lblSeperator3.BackColor = StockProfile.Colors.AxisLineColor;
                lblSeperator4.BackColor = StockProfile.Colors.AxisLineColor;

                lastBuyVolume1 = 0;
                lastBuyVolume2 = 0;
                lastBuyVolume3 = 0;
                lastBuyVolume4 = 0;
                lastBuyVolume5 = 0;
                lastSellVolume1 = 0;
                lastSellVolume2 = 0;
                lastSellVolume3 = 0;
                lastSellVolume4 = 0;
                lastSellVolume5 = 0;

                if (value != null)
                {
                    lblStock.Text = string.Format("{0} {1}", value.Code, value.Name);

                    lblShareCountTotal.Text = string.Format("{0:#0}", stock.ShareCountTotal);
                    lblShareCountA.Text = string.Format("{0:#0}", stock.ShareCountA);
                    lblNetAssetPs.Text = string.Format("{0:#0.00}", stock.NetAssetPs);
                    lblPe.Text = string.Format("{0:#0.00}", stock.Pe);

                    lblShareCountTotal.ForeColor = StockProfile.Colors.GraphTextColor;
                    lblShareCountA.ForeColor = StockProfile.Colors.GraphTextColor;
                    lblNetAssetPs.ForeColor = StockProfile.Colors.GraphTextColor;
                    lblPe.ForeColor = StockProfile.Colors.GraphTextColor;

                    UpdateData();
                }
            }
        }


        public ControlTrade()
        {
            InitializeComponent();

            gridMinute.AutoGenerateColumns = false;
            gridMinute.BackgroundColor = StockProfile.Colors.BackColor;
            gridMinute.DefaultCellStyle.BackColor = StockProfile.Colors.BackColor;
            gridMinute.DefaultCellStyle.ForeColor = StockProfile.Colors.GridTextColor;
            gridMinute.Columns["Volume"].DefaultCellStyle.ForeColor = StockProfile.Colors.VolumeTextColor;
            gridMinute.DefaultCellStyle.SelectionBackColor = StockProfile.Colors.BackColor;

            StockContainer.Instance.DataChanged += new StockContainerDataEventHandler(StockContainer_DataChanged);
        }

        private void StockContainer_DataChanged(object sender, EventArgs e)
        {
            UpdateData();
        }

        private float lastBuyVolume1 = 0;
        private float lastBuyVolume2 = 0;
        private float lastBuyVolume3 = 0;
        private float lastBuyVolume4 = 0;
        private float lastBuyVolume5 = 0;
        private float lastSellVolume1 = 0;
        private float lastSellVolume2 = 0;
        private float lastSellVolume3 = 0;
        private float lastSellVolume4 = 0;
        private float lastSellVolume5 = 0;

        private void UpdateData()
        {
            if (this._stock != null && StockContainer.Instance.ContainsKey(this._stock.CodeKey))
            {
                lblBuyPrice1.Text = string.Format("{0:#0.00}", stock.BuyPrice[0]);
                lblBuyPrice2.Text = string.Format("{0:#0.00}", stock.BuyPrice[1]);
                lblBuyPrice3.Text = string.Format("{0:#0.00}", stock.BuyPrice[2]);
                lblBuyPrice4.Text = string.Format("{0:#0.00}", stock.BuyPrice[3]);
                lblBuyPrice5.Text = string.Format("{0:#0.00}", stock.BuyPrice[4]);
                lblBuyVolume1.Text = string.Format("{0:#0}", stock.BuyVolume[0]);
                lblBuyVolume2.Text = string.Format("{0:#0}", stock.BuyVolume[1]);
                lblBuyVolume3.Text = string.Format("{0:#0}", stock.BuyVolume[2]);
                lblBuyVolume4.Text = string.Format("{0:#0}", stock.BuyVolume[3]);
                lblBuyVolume5.Text = string.Format("{0:#0}", stock.BuyVolume[4]);
                lblSellPrice1.Text = string.Format("{0:#0.00}", stock.SellPrice[0]);
                lblSellPrice2.Text = string.Format("{0:#0.00}", stock.SellPrice[1]);
                lblSellPrice3.Text = string.Format("{0:#0.00}", stock.SellPrice[2]);
                lblSellPrice4.Text = string.Format("{0:#0.00}", stock.SellPrice[3]);
                lblSellPrice5.Text = string.Format("{0:#0.00}", stock.SellPrice[4]);
                lblSellVolume1.Text = string.Format("{0:#0}", stock.SellVolume[0]);
                lblSellVolume2.Text = string.Format("{0:#0}", stock.SellVolume[1]);
                lblSellVolume3.Text = string.Format("{0:#0}", stock.SellVolume[2]);
                lblSellVolume4.Text = string.Format("{0:#0}", stock.SellVolume[3]);
                lblSellVolume5.Text = string.Format("{0:#0}", stock.SellVolume[4]);
                lblNow.Text = string.Format("{0:#0.00}", stock.New);
                lblOpen.Text = string.Format("{0:#0.00}", stock.Open);
                lblDiff.Text = string.Format("{0:#0.00}", stock.Diff);
                lblDiffRatio.Text = string.Format("{0:#0.00}", stock.Amplitude);
                lblHigh.Text = string.Format("{0:#0.00}", stock.High);
                lblLow.Text = string.Format("{0:#0.00}", stock.Low);
                lblVolume.Text = string.Format("{0:#0}", stock.Volume);

                /*lblBuyVolumeDiff1.Text = string.Format("{0:#0}", stock.BuyVolume[0] - lastBuyVolume1);
                lblBuyVolumeDiff2.Text = string.Format("{0:#0}", stock.BuyVolume[1] - lastBuyVolume2);
                lblBuyVolumeDiff3.Text = string.Format("{0:#0}", stock.BuyVolume[2] - lastBuyVolume3);
                lblBuyVolumeDiff4.Text = string.Format("{0:#0}", stock.BuyVolume[3] - lastBuyVolume4);
                lblBuyVolumeDiff5.Text = string.Format("{0:#0}", stock.BuyVolume[4] - lastBuyVolume5);

                lblSellVolumeDiff1.Text = string.Format("{0:#0}", stock.SellVolume[0] - lastSellVolume1);
                lblSellVolumeDiff2.Text = string.Format("{0:#0}", stock.SellVolume[1] - lastSellVolume2);
                lblSellVolumeDiff3.Text = string.Format("{0:#0}", stock.SellVolume[2] - lastSellVolume3);
                lblSellVolumeDiff4.Text = string.Format("{0:#0}", stock.SellVolume[3] - lastSellVolume4);
                lblSellVolumeDiff5.Text = string.Format("{0:#0}", stock.SellVolume[4] - lastSellVolume5);*/

                lblBuyPrice1.ForeColor = GetColor(stock.BuyPrice[0], stock.Close);
                lblBuyPrice2.ForeColor = GetColor(stock.BuyPrice[1], stock.Close);
                lblBuyPrice3.ForeColor = GetColor(stock.BuyPrice[2], stock.Close);
                lblBuyPrice4.ForeColor = GetColor(stock.BuyPrice[3], stock.Close);
                lblBuyPrice5.ForeColor = GetColor(stock.BuyPrice[4], stock.Close);
                lblSellPrice1.ForeColor = GetColor(stock.SellPrice[0], stock.Close);
                lblSellPrice2.ForeColor = GetColor(stock.SellPrice[1], stock.Close);
                lblSellPrice3.ForeColor = GetColor(stock.SellPrice[2], stock.Close);
                lblSellPrice4.ForeColor = GetColor(stock.SellPrice[3], stock.Close);
                lblSellPrice5.ForeColor = GetColor(stock.SellPrice[4], stock.Close);
                lblNow.ForeColor = GetColor(stock.New, stock.Close);
                lblDiff.ForeColor = lblNow.ForeColor;
                lblDiffRatio.ForeColor = lblNow.ForeColor;
                lblHigh.ForeColor = GetColor(stock.High, stock.Close);
                lblLow.ForeColor = GetColor(stock.Low, stock.Close);

                lblBuyVolume1.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblBuyVolume2.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblBuyVolume3.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblBuyVolume4.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblBuyVolume5.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblSellVolume1.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblSellVolume2.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblSellVolume3.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblSellVolume4.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblSellVolume5.ForeColor = StockProfile.Colors.VolumeTextColor;
                lblVolume.ForeColor = StockProfile.Colors.VolumeTextColor;

                lblBuyVolumeDiff1.ForeColor = GetColor(stock.BuyVolume[0], lastBuyVolume1);
                lblBuyVolumeDiff2.ForeColor = GetColor(stock.BuyVolume[1], lastBuyVolume2);
                lblBuyVolumeDiff3.ForeColor = GetColor(stock.BuyVolume[2], lastBuyVolume3);
                lblBuyVolumeDiff4.ForeColor = GetColor(stock.BuyVolume[3], lastBuyVolume4);
                lblBuyVolumeDiff5.ForeColor = GetColor(stock.BuyVolume[4], lastBuyVolume5);

                lblSellVolumeDiff1.ForeColor = GetColor(stock.SellVolume[0], lastSellVolume1);
                lblSellVolumeDiff2.ForeColor = GetColor(stock.SellVolume[1], lastSellVolume2);
                lblSellVolumeDiff3.ForeColor = GetColor(stock.SellVolume[2], lastSellVolume3);
                lblSellVolumeDiff4.ForeColor = GetColor(stock.SellVolume[3], lastSellVolume4);
                lblSellVolumeDiff5.ForeColor = GetColor(stock.SellVolume[4], lastSellVolume5);

                lastBuyVolume1 = stock.BuyVolume[0];
                lastBuyVolume2 = stock.BuyVolume[1];
                lastBuyVolume3 = stock.BuyVolume[2];
                lastBuyVolume4 = stock.BuyVolume[3];
                lastBuyVolume5 = stock.BuyVolume[4];
                lastSellVolume1 = stock.SellVolume[0];
                lastSellVolume2 = stock.SellVolume[1];
                lastSellVolume3 = stock.SellVolume[2];
                lastSellVolume4 = stock.SellVolume[3];
                lastSellVolume5 = stock.SellVolume[4];
            }
        }

        private void gridMinute_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (stock == null)
                return;

            DataGridViewCell cell = gridMinute.Rows[e.RowIndex].Cells[e.ColumnIndex];
            float high = Convert.ToSingle(cell.OwningRow.Cells["High"].Value);
            float diff = high - stock.Close;

            switch (cell.OwningColumn.Name)
            {
                case "High":
                    if (diff > 0)
                        e.CellStyle.ForeColor = StockProfile.Colors.UpTextColor;
                    else if (diff < 0)
                        e.CellStyle.ForeColor = StockProfile.Colors.DnTextColor;
                    else
                        e.CellStyle.ForeColor = StockProfile.Colors.GridTextColor;

                    e.Value = string.Format("{0:#0.00}", high);
                    break;
            }

            if (gridMinute.Rows[e.RowIndex].Selected)
            {
                e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor;
            }
        }

        private Color GetColor(float price1, float price2)
        {
            if (price1 > price2)
                return StockProfile.Colors.UpTextColor;

            if (price1 < price2)
                return StockProfile.Colors.DnTextColor;

            return StockProfile.Colors.StTextColor;
        }
    }
}
